package com.atguigu.lease.web.app.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.web.app.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private Client client;


    @Override
    public void sendCode(String phone, String verifyCode) {
        SendSmsRequest smsRequest = new SendSmsRequest();
        smsRequest.setSignName("阿里云短信测试");
        smsRequest.setTemplateCode("SMS_154950909");
        smsRequest.setPhoneNumbers(phone);
        String code = "{\"code\":\""+verifyCode+"\"}";
        smsRequest.setTemplateParam(code);

        //4.获取响应信息
        try {
            SendSmsResponse sendSmsResponse = client.sendSms(smsRequest);
            Integer statusCode = sendSmsResponse.getStatusCode();
            if(statusCode != 200){
                throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_ERROR);
            }
        } catch (Exception e) {
            throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_ERROR);
        }
    }
}
